package com.pokerpro.Scanner;

import static com.pokerpro.support.Log.log;
import static com.pokerpro.Constant.Colors.*;
import static com.pokerpro.Constant.Positions.*;

import java.awt.Color;
import java.awt.Point;
import java.awt.Robot;
import java.util.ArrayList;
import java.util.List;

import com.pokerpro.interfaces.TableEvents;
import com.pokerpro.model.Card;
import com.pokerpro.support.BaseElement;

public class BoardScanner extends BaseElement implements Runnable {

	TableEvents ev;
	boolean flop = false;
	boolean turn = false;
	boolean river = false;
	
	private Point boardCard1 ;
	private Point boardCard2 ;
	private Point boardCard3 ;
	private Point boardCard4 ;
	private Point boardCard5 ;

	public BoardScanner(Robot pRobot, List<Point> list, TableEvents e) {
		super(pRobot);
		ev = e;
		boardCard1 = list.get(0);
		boardCard2 = list.get(1);
		boardCard3 = list.get(2);
		boardCard4 = list.get(3);
		boardCard5 = list.get(4);
	}

	@Override
	public void run() {
		log("Board Scanner runs");
		while (true) {
			if (!isVisible("flop")) {
				flop = false;
				turn = false;
				river = false;
			}
			if (isVisible("flop") && flop==false) {
				log("New Flop arrived");
				List<Card> list = new ArrayList<Card>();
				list.add(scanCard(boardCard1));
				list.add(scanCard(boardCard2));
				list.add(scanCard(boardCard3));
				ev.newFlop(list);
				flop = true;
			}
			if (isVisible("turn") && turn == false) {
				log("New Turn arrived");
				ev.newTurn(scanCard(boardCard4));
				turn = true;
			}
			if (isVisible("river") && river == false) {
				log("New River arrived");
				ev.newRiver(scanCard(boardCard5));
				river = true;
			}
		}
	}

	public boolean isBoardPresent() {
		return isVisible("flop");
	}

	public Card scanCard(Point position) {
		log("Scan Board-Card");
		Color cardColor = getPixelCol(position);
		Card card = new Card();

		/*
		 * 2er
		 */
		if (cardColor.equals(COLOR_CARD_2_CLUB))
			card = new Card("2:club");

		if (cardColor.equals(COLOR_CARD_2_DIAMOND))
			card = new Card("2:diamond");

		if (cardColor.equals(COLOR_CARD_2_HEART))
			card = new Card("2:heart");

		if (cardColor.equals(COLOR_CARD_2_SPADE))
			card = new Card("2:spade");

		/*
		 * 3er
		 */
		if (cardColor.equals(COLOR_CARD_3_CLUB))
			card = new Card("3:club");

		if (cardColor.equals(COLOR_CARD_3_DIAMOND))
			card = new Card("3:diamond");

		if (cardColor.equals(COLOR_CARD_3_HEART))
			card = new Card("3:heart");

		if (cardColor.equals(COLOR_CARD_3_SPADE))
			card = new Card("3:spade");

		/*
		 * 4er
		 */

		if (cardColor.equals(COLOR_CARD_4_CLUB))
			card = new Card("4:club");

		if (cardColor.equals(COLOR_CARD_4_DIAMOND))
			card = new Card("4:diamond");

		if (cardColor.equals(COLOR_CARD_4_HEART))
			card = new Card("4:heart");

		if (cardColor.equals(COLOR_CARD_4_SPADE))
			card = new Card("4:spade");

		/*
		 * 5er
		 */
		if (cardColor.equals(COLOR_CARD_5_CLUB))
			card = new Card("5:club");

		if (cardColor.equals(COLOR_CARD_5_DIAMOND))
			card = new Card("5:diamond");

		if (cardColor.equals(COLOR_CARD_5_HEART))
			card = new Card("5:heart");

		if (cardColor.equals(COLOR_CARD_5_SPADE))
			card = new Card("5:spade");

		/*
		 * 6er
		 */
		if (cardColor.equals(COLOR_CARD_6_CLUB))
			card = new Card("6:club");

		if (cardColor.equals(COLOR_CARD_6_DIAMOND))
			card = new Card("6:diamond");

		if (cardColor.equals(COLOR_CARD_6_HEART))
			card = new Card("6:heart");

		if (cardColor.equals(COLOR_CARD_6_SPADE))
			card = new Card("6:spade");

		/*
		 * 7er
		 */
		if (cardColor.equals(COLOR_CARD_7_CLUB))
			card = new Card("7:club");

		if (cardColor.equals(COLOR_CARD_7_DIAMOND))
			card = new Card("7:diamond");

		if (cardColor.equals(COLOR_CARD_7_HEART))
			card = new Card("7:heart");

		if (cardColor.equals(COLOR_CARD_7_SPADE))
			card = new Card("7:spade");

		/*
		 * 8er
		 */
		if (cardColor.equals(COLOR_CARD_8_CLUB))
			card = new Card("8:club");

		if (cardColor.equals(COLOR_CARD_8_DIAMOND))
			card = new Card("8:diamond");

		if (cardColor.equals(COLOR_CARD_2_HEART))
			card = new Card("8:heart");

		if (cardColor.equals(COLOR_CARD_8_SPADE))
			card = new Card("8:spade");

		/*
		 * 9er
		 */
		if (cardColor.equals(COLOR_CARD_9_CLUB))
			card = new Card("9:club");

		if (cardColor.equals(COLOR_CARD_9_DIAMOND))
			card = new Card("9:diamond");

		if (cardColor.equals(COLOR_CARD_9_HEART))
			card = new Card("9:heart");

		if (cardColor.equals(COLOR_CARD_9_SPADE))
			card = new Card("9:spade");

		/*
		 * 10er
		 */
		if (cardColor.equals(COLOR_CARD_10_CLUB))
			card = new Card("10:club");

		if (cardColor.equals(COLOR_CARD_10_DIAMOND))
			card = new Card("10:diamond");

		if (cardColor.equals(COLOR_CARD_10_HEART))
			card = new Card("10:heart");

		if (cardColor.equals(COLOR_CARD_10_SPADE))
			card = new Card("10:spade");

		/*
		 * Jacks
		 */
		if (cardColor.equals(COLOR_CARD_J_CLUB))
			card = new Card("jack:club");

		if (cardColor.equals(COLOR_CARD_J_DIAMOND))
			card = new Card("jack:diamond");

		if (cardColor.equals(COLOR_CARD_J_HEART))
			card = new Card("jack:heart");

		if (cardColor.equals(COLOR_CARD_J_SPADE))
			card = new Card("jack:spade");

		/*
		 * Queens
		 */
		if (cardColor.equals(COLOR_CARD_Q_CLUB))
			card = new Card("queen:club");

		if (cardColor.equals(COLOR_CARD_Q_DIAMOND))
			card = new Card("queen:diamond");

		if (cardColor.equals(COLOR_CARD_Q_HEART))
			card = new Card("queen:heart");

		if (cardColor.equals(COLOR_CARD_Q_SPADE))
			card = new Card("queen:spade");

		/*
		 * Kings
		 */
		if (cardColor.equals(COLOR_CARD_K_CLUB))
			card = new Card("king:club");

		if (cardColor.equals(COLOR_CARD_K_DIAMOND))
			card = new Card("king:diamond");

		if (cardColor.equals(COLOR_CARD_K_HEART))
			card = new Card("king:heart");

		if (cardColor.equals(COLOR_CARD_K_SPADE))
			card = new Card("king:spade");

		/*
		 * Aces
		 */
		if (cardColor.equals(COLOR_CARD_A_CLUB))
			card = new Card("ace:club");

		if (cardColor.equals(COLOR_CARD_A_DIAMOND))
			card = new Card("ace:diamond");

		if (cardColor.equals(COLOR_CARD_A_HEART))
			card = new Card("ace:heart");

		if (cardColor.equals(COLOR_CARD_A_SPADE))
			card = new Card("ace:spade");

		return card;
	}

	public boolean isVisible(String indicator) {
		Color acutalColor = new Color(1, 1, 1);
		if (indicator.equals("flop"))
			acutalColor = getPixelCol(TABLE1_COMMUNITYCARD_3);
		else if (indicator.equals("turn"))
			acutalColor = getPixelCol(TABLE1_COMMUNITYCARD_4);
		else if (indicator.equals("river"))
			acutalColor = getPixelCol(TABLE1_COMMUNITYCARD_5);

		if (acutalColor.equals(COLOR_CARD_2_SPADE)
				|| acutalColor.equals(COLOR_CARD_2_HEART)
				|| acutalColor.equals(COLOR_CARD_2_DIAMOND)
				|| acutalColor.equals(COLOR_CARD_2_CLUB)
				|| acutalColor.equals(COLOR_CARD_3_SPADE)
				|| acutalColor.equals(COLOR_CARD_3_HEART)
				|| acutalColor.equals(COLOR_CARD_3_DIAMOND)
				|| acutalColor.equals(COLOR_CARD_3_CLUB)
				|| acutalColor.equals(COLOR_CARD_4_SPADE)
				|| acutalColor.equals(COLOR_CARD_4_HEART)
				|| acutalColor.equals(COLOR_CARD_4_DIAMOND)
				|| acutalColor.equals(COLOR_CARD_4_CLUB)
				|| acutalColor.equals(COLOR_CARD_5_SPADE)
				|| acutalColor.equals(COLOR_CARD_5_HEART)
				|| acutalColor.equals(COLOR_CARD_5_DIAMOND)
				|| acutalColor.equals(COLOR_CARD_5_CLUB)
				|| acutalColor.equals(COLOR_CARD_6_SPADE)
				|| acutalColor.equals(COLOR_CARD_6_HEART)
				|| acutalColor.equals(COLOR_CARD_6_DIAMOND)
				|| acutalColor.equals(COLOR_CARD_6_CLUB)
				|| acutalColor.equals(COLOR_CARD_7_SPADE)
				|| acutalColor.equals(COLOR_CARD_7_HEART)
				|| acutalColor.equals(COLOR_CARD_7_DIAMOND)
				|| acutalColor.equals(COLOR_CARD_7_CLUB)
				|| acutalColor.equals(COLOR_CARD_8_SPADE)
				|| acutalColor.equals(COLOR_CARD_8_HEART)
				|| acutalColor.equals(COLOR_CARD_8_DIAMOND)
				|| acutalColor.equals(COLOR_CARD_8_CLUB)
				|| acutalColor.equals(COLOR_CARD_9_SPADE)
				|| acutalColor.equals(COLOR_CARD_9_HEART)
				|| acutalColor.equals(COLOR_CARD_9_DIAMOND)
				|| acutalColor.equals(COLOR_CARD_9_CLUB)
				|| acutalColor.equals(COLOR_CARD_10_SPADE)
				|| acutalColor.equals(COLOR_CARD_10_HEART)
				|| acutalColor.equals(COLOR_CARD_10_DIAMOND)
				|| acutalColor.equals(COLOR_CARD_10_CLUB)
				|| acutalColor.equals(COLOR_CARD_J_SPADE)
				|| acutalColor.equals(COLOR_CARD_J_HEART)
				|| acutalColor.equals(COLOR_CARD_J_DIAMOND)
				|| acutalColor.equals(COLOR_CARD_J_CLUB)
				|| acutalColor.equals(COLOR_CARD_Q_SPADE)
				|| acutalColor.equals(COLOR_CARD_Q_HEART)
				|| acutalColor.equals(COLOR_CARD_Q_DIAMOND)
				|| acutalColor.equals(COLOR_CARD_Q_CLUB)
				|| acutalColor.equals(COLOR_CARD_K_SPADE)
				|| acutalColor.equals(COLOR_CARD_K_HEART)
				|| acutalColor.equals(COLOR_CARD_K_DIAMOND)
				|| acutalColor.equals(COLOR_CARD_K_CLUB)
				|| acutalColor.equals(COLOR_CARD_A_SPADE)
				|| acutalColor.equals(COLOR_CARD_A_HEART)
				|| acutalColor.equals(COLOR_CARD_A_DIAMOND)
				|| acutalColor.equals(COLOR_CARD_A_CLUB)) {
			return true;
		} else
			return false;
	}
}
